import React from 'react';
import GenericTable from '../components/tables/GenericTable';
import { Tag, Switch, message } from 'antd';
import '../css/lzk.css';
import dayjs from 'dayjs';
import axios from 'axios';

axios.defaults.baseURL = 'http://localhost:3000/';


// 处理邀请操作的函数
const handleInvite = (record) => {
    console.log(`Inviting ${record.name}`);
};

const handleDelete = async (id, onReload) => {
    try {
        await axios.get('/kai/delfield', { params: { _id: id } });
        message.error('删除成功');
        if (onReload) {
            onReload(); // 调用父组件传递的函数，触发重新渲染
        }
    } catch (error) {
        message.error('删除失败');
    }
};

const DataProvider = (props) => {
    // 定义表格列配置
const columns = [
    {
        align: 'center',
        title: '序号',
        dataIndex: 'hua',
        key: '_id',
        render: (_, record, index) => (
            <span>{index + 1}</span>
        )
    },
    {
        align: 'center',
        title: '字段名称',
        key: '_id',
        dataIndex: 'name',
        render: (text) => (
            <span>{text}</span>
        )
    },
    {
        align: 'center',
        title: '字段类型',
        key: '_id',
        dataIndex: 'lei',
        render: (text) => (
            <span>{text}</span>
        )
    },
    {
        width: '3rem',
        align: 'center',
        title: '是否必填',
        key: '_id',
        render: (_, record) => (
            <span>{record.tian ? '必填' : '非必填'}</span>
        )
    },
    {
        align: 'center',
        title: '最后更新人',
        key: '_id',
        dataIndex: 'ren',
        render: (_, record) => (
            <span>{record.ren}</span>
        )
    },
    {
        align: 'center',
        title: '最后更新时间',
        key: '_id',
        dataIndex: 'end_time',
        render: (_, record) => (
            dayjs(record.end_time).format('YYYY-MM-DD HH:mm:ss')
        )
    },
    {
        align: 'center',
        title: '状态',
        key: '_id',
        render: (_, record) => (
            <Switch checkedChildren="启用" unCheckedChildren="禁用" checked={record.status} />
        )
    },
    {
        align: 'center',
        title: '操作',
        render: (_, record) => (
            <span>
                <a style={{ marginLeft: 16 }}>
                    编辑
                </a>
                <a onClick={() => handleDelete(record._id, props.onReload)} style={{ marginLeft: 16, color: 'red' }}>
                    删除
                </a>
            </span>
        )
    },
];

    return (
        <GenericTable
            columns={columns}
            dataSource={props.fieldList}
            onInvite={handleInvite}
            // onDelete={handleDelete}
        />
    );
};

export default DataProvider;